package com.android.customized;

import com.dxyxx.R;
import com.nostra13.example.universalimageloader.ScaleBitmapDisplayer;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HorizontalListViewAdapter extends BaseAdapter {
	private String[] mIconIDs;
	private String[] mSpecifications;
	private String[] mTitles;
	private String[] mPrices;
	private Context mContext;
	private LayoutInflater mInflater;
	Bitmap iconBitmap;
	private ImageLoader imageLoader;
	private int selectIndex = -1;

	public HorizontalListViewAdapter(Context context, String[] titles, String[] images, String[] specifications,String[] prices,ImageLoader imageLoader) {
		this.mContext = context;
		this.mIconIDs = images;
		this.mTitles = titles;
		this.mSpecifications = specifications;
		this.imageLoader = imageLoader;
		this.mPrices = prices;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return mIconIDs.length;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	protected DisplayImageOptions squareOptions = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_stub).showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(R.drawable.ic_error).cacheInMemory(true).cacheOnDisc(true).considerExifParams(true).displayer(new ScaleBitmapDisplayer()).build();

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_buyproduct_horizontallist, null);
			holder.mImage = (ImageView) convertView.findViewById(R.id.img_list_item);
			holder.specification = (TextView) convertView.findViewById(R.id.textSpecification);
			holder.price = (TextView) convertView.findViewById(R.id.textPrice);
			holder.mTitle = (TextView) convertView.findViewById(R.id.textName);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (position == selectIndex) {
			convertView.setSelected(true);
		} else {
			convertView.setSelected(false);
		}

		holder.mTitle.setText(mTitles[position]);
		holder.specification.setText(mSpecifications[position]);
		holder.price.setText(mPrices[position]);
		//holder.specification.setText(mTitles[position]);
		//holder.price.setText(mTitles[position]);
		//		iconBitmap = getPropThumnail(mIconIDs[position]);

		holder.mImage.setImageResource(R.drawable.core_homepage_arrival_scan);
		imageLoader.displayImage(mIconIDs[position], holder.mImage, squareOptions, null);

		return convertView;
	}

	private static class ViewHolder {
		private TextView mTitle;
		private TextView specification;
		private TextView price;
		private ImageView mImage;
	}

	//	private Bitmap getPropThumnail(int id){
	//		Drawable d = mContext.getResources().getDrawable(id);
	//		Bitmap b = BitmapUtil.drawableToBitmap(d);
	////		Bitmap bb = BitmapUtil.getRoundedCornerBitmap(b, 100);
	//		int w = mContext.getResources().getDimensionPixelOffset(R.dimen.thumnail_default_width);
	//		int h = mContext.getResources().getDimensionPixelSize(R.dimen.thumnail_default_height);
	//		
	//		Bitmap thumBitmap = ThumbnailUtils.extractThumbnail(b, w, h);
	//		
	//		return thumBitmap;
	//	}
	public void setSelectIndex(int i) {
		selectIndex = i;
	}
}